package com.oocl.im.server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;

import com.oocl.im.handler.RegisterHandler;

public class RegisterServer implements Runnable {
	private final int PORT = 8400;
	private ExecutorService threadPool;
	private ServerSocket serverSocket;

	public RegisterServer() {
		super();
	}

	public RegisterServer(ExecutorService threadPool) {
		super();
		this.threadPool = threadPool;
		try {
			this.serverSocket = new ServerSocket(PORT);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void run() {
		Socket socket;
		try {
			while (true) {

				socket = serverSocket.accept();
				threadPool.submit(new RegisterHandler(socket));
			}
		} catch (IOException e) {
			System.out.println("register server close");
		}
	}

	public void close() {
		try {
			serverSocket.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
